package com.bjpowernode.controller;

import com.alibaba.excel.EasyExcel;
import com.bjpowernode.config.listener.ClueDataListener;
import com.bjpowernode.entity.TClue;
import com.bjpowernode.entity.TClueDO;
import com.bjpowernode.entity.TUser;
import com.bjpowernode.query.ClueQuery;
import com.bjpowernode.result.R;
import com.bjpowernode.service.ClueService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;

@RestController
public class ClueController {

    @Resource
    private ClueService clueService;

    @GetMapping(value = "/api/clues")
    public R clueByPage(@RequestParam(value = "current", required = false) Integer current) {
        // required = false 表示current参数可以传，也可以不传，也就是current参数可以是空的；
        // required = true 表示current参数必须传，不传会报错；
        if (current == null) {
            current = 1;
        }
        PageInfo<TClue> pageInfo = clueService.getCluePage(current);
        return R.OK(pageInfo);
    }

    @PostMapping(value = "/api/importExcel")
    public R importExcel(MultipartFile excelFile) throws IOException { //参数名要和前端提交的参数名相同
        System.out.println("导入Excel...................");

        InputStream excelFileInputStream = excelFile.getInputStream();
        clueService.importExcel(excelFileInputStream);

        return R.OK();
    }

    @GetMapping(value = "/api/clue/{phone}")
    public R checkPhone(@PathVariable(value = "phone") String phone) {
        int count = clueService.checkPhone(phone);
        return count <= 0 ? R.OK() : R.FAIL();
    }

    @PostMapping(value = "/api/clue")
    public R addClue(@Valid ClueQuery clueQuery) {
        //参数验证，页面跳转，返回数据这些业务操作，其他业务操作通过是写到service里面
        int count = clueService.checkPhone(clueQuery.getPhone());
        if (count >= 1) {
            //表示数据库已经存在该手机号
            return R.FAIL("该手机号已经录入过了，不能再录入");
        }
        int save = clueService.saveClue(clueQuery);
        return save >= 1 ? R.OK() : R.FAIL();
    }

    @GetMapping(value = "/api/clue/info/{clueId}")
    public R clueInfo(@PathVariable(value = "clueId") Integer clueId) {
        TClue tClue = clueService.getClueById(clueId);
        return R.OK(tClue);
    }

    @PutMapping(value = "/api/clue")
    public R editClue(@Valid ClueQuery clueQuery) {
        int update = clueService.updateClue(clueQuery);
        return update >= 1 ? R.OK() : R.FAIL();
    }

    @DeleteMapping(value = "/api/clue/{id}")
    public R delActivity(@PathVariable(value = "id") Integer clueId) {
        int del = clueService.delClueById(clueId);
        return del >= 1 ? R.OK() : R.FAIL();
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping(value = "/api/clue")
    public R batchDelActivity(@RequestParam(value = "ids") String ids) {
        // ids = "1,3,5,6,8,12";
        List<String> idList = Arrays.asList(ids.split(","));
        int del = clueService.batchDelClueByIds(idList);
        return del >= idList.size() ? R.OK() : R.FAIL();
    }
}
